#include <QFileInfo>
#include <iostream>
#include "imageWidget.h"
#include "ui_imageWidget.h"

ImageWidget::ImageWidget(QWidget *parent) :
  QWidget(parent),
  ui(new Ui::ImageWidget) {
  ui->setupUi(this);
  ui->radioButtonAll->toggle();
  ui->controlsFrame->hide();
  hideAllViews();
  //  QImage img("://cat.pgm");
}

ImageWidget::~ImageWidget() {
  delete ui;
}

bool ImageWidget::loadImage(const QString & filename) {
  if(filename.isEmpty()) {
    qDebug() << "Input string is empty";
    return false;
  }
  QFileInfo fInfo (filename);
  if(!fInfo.exists()) {
    qDebug() << filename << " filename does not existis.";
    return false;
  }
  if(fInfo.completeSuffix() != "dcm" && !fInfo.completeSuffix().isEmpty()) {
    qDebug() << filename << " suffix is not dcm";
    return false;
  }
  gdcm::ImageReader ir;
  ir.SetFileName( fInfo.absoluteFilePath().toUtf8() );
  if(!ir.Read()) {
    qDebug() << "Read failed";
    return false;
  }
  std::cout<<"Getting image from ImageReader..."<<std::endl;
  gimage = ir.GetImage();
  qDebug() << "SPACING = (" << gimage.GetSpacing()[0] << ", " << gimage.GetSpacing()[1] << ");";
  std::cout << "DATAELEMENT: \n" << gimage.GetDataElement() << std::endl;
  const gdcm::File & file = ir.GetFile();
  if( info != NULL )
    info->Open(file);
  if(gimage.IsEmpty()) {
    std::cerr << "empty image!";
    return false;
  }

  return true;
}

bool ImageWidget::showImage(const QString & filename) {
  if(!loadImage(filename)) {
    qDebug() << "Could not open image.";
    return false;
  }
  return show2D();
}

bool ImageWidget::show2D() {
  std::vector<char> vbuffer;
  vbuffer.resize( gimage.GetBufferLength() );
  char *buffer = &vbuffer[0];
  QImage *imageQt = NULL;

  if( !GDCMQT::ConvertToFormat_RGB888( gimage, buffer, imageQt ) ) {
    qDebug() << "ConvertToFormat error";
    return false;
  }
  hideAllViews();
  ui->controlsFrame->hide();
  ui->axialWidget->show();
  ui->axialSlider->hide();
  ui->playAxialButtom->hide();
  ui->axialView->showImage(*imageQt);
  return true;
}

bool ImageWidget::show3D() {
  std::vector<char> vbuffer;
  vbuffer.resize( gimage.GetBufferLength() );
  char *buffer = &vbuffer[0];
  QImage *imageQt = NULL;
  if( !GDCMQT::ConvertToFormat_RGB888( gimage, buffer, imageQt ) ) {
    qDebug() << "ConvertToFormat error";
    return false;
  }
  showAllViews();
  ui->controlsFrame->show();
  return true;
}

bool ImageWidget::showCine() {
  std::vector<char> vbuffer;
  vbuffer.resize( gimage.GetBufferLength() );
  char *buffer = &vbuffer[0];
  QImage *imageQt = NULL;

  if( !GDCMQT::ConvertToFormat_RGB888( gimage, buffer, imageQt ) ) {
    qDebug() << "ConvertToFormat error";
    return false;
  }
  hideAllViews();
  ui->controlsFrame->hide();
  showAxial();
  ui->axialView->showImage(*imageQt);
  return true;
}

void ImageWidget::on_radioButtonAxial_clicked() {
  hideAllViews();
  showAxial();
}

void ImageWidget::on_radioButtonCoronal_clicked() {
  hideAllViews();
  ui->coronalWidget->show();
}

void ImageWidget::on_radioButtonSagittal_clicked() {
  hideAllViews();
  ui->sagittalWidget->show();
}

void ImageWidget::on_radioButtonAll_clicked() {
  showAllViews();
}

StudyInfo * ImageWidget::getInfo() const {
  return info;
}

void ImageWidget::setInfo(StudyInfo * value) {
  info = value;
}

void ImageWidget::showAxial() {
  ui->axialWidget->show();
  ui->axialSlider->show();
  ui->playAxialButtom->show();
}

void ImageWidget::showAllViews() {
  showAxial();
  ui->coronalWidget->show();
  ui->sagittalWidget->show();
}

void ImageWidget::hideAllViews() {
  ui->axialWidget->hide();
  ui->coronalWidget->hide();
  ui->sagittalWidget->hide();
}

void ImageWidget::receiveImagePath(QTreeWidgetItem* item, int column) {
  qDebug() << item->text(2);
  showImage(item->text(2));
}

